#include "trace_camera.h"
#include "settings.h"
#include "sdl_screen.h"


TraceCamera::TraceCamera(const glm::vec3& pos, float focal)
: Camera(pos, focal)
{
	sdl::Event::getInstance()->addListener(this);
}


glm::vec3 TraceCamera::getDirection(int x, int y) const
{
	const sdl::Screen* s = sdl::Screen::getInstance();
	return _rotYMatrix * glm::vec3(
		x - s->getWidth() / 2.f, y - s->getHeight() / 2.f, _focal);
}


void TraceCamera::on(EventListener::NewEvent, const SDL_Event* e) throw()
{
	if (e->type != SDL_KEYDOWN)
		return;
	switch (e->key.keysym.sym) {
	case CAMERA_MOVE_FORW:
		// Move camera forward
		transform(glm::vec3(0, 0, CAMERA_MOVE_STEP));
		break;
	case CAMERA_MOVE_BACK:
		// Move camera backward
		transform(glm::vec3(0, 0, -CAMERA_MOVE_STEP));
		break;
	case CAMERA_MOVE_LEFT:
		// Move camera to the left
		rotateY(CAMERA_ROTATE_ANGLE);
		break;
	case CAMERA_MOVE_RIGHT:
		// Move camera to the right
		rotateY(-CAMERA_ROTATE_ANGLE);
		break;
	default:
		return;
	}
	// Update screen
	sdl::Screen::getInstance()->update();
}

